{% extends "common/base.html" %}

{% block title %}上传 - {{ block.Super }}{% endblock %}

{% block wrapper %}
<style>
.main-box {
    margin-bottom: 0;
    margin: 15px;
}
.main-box .main-box-body {
    padding: 20px;
}
@media screen and (max-width: 767px) {
    .table-responsive {
        margin-bottom: 0;
    }
}
.select-file {
    cursor: pointer;
}
</style>

<script src='{{ assets("js/jquery.js") }}' type="text/javascript"></script>
<script src='{{ assets("js/layer/layer.js") }}' type="text/javascript"></script>

<div class="main-box clearfix">
    <div class="main-box-body clearfix">
        <form role="form" class="fs-form">
            <div class="form-group">
                <label for="file-content">选择文件</label>
                <div class="input-group">
                    <input type="text" class="form-control" id="location" readonly>
                    <label class="input-group-addon select-file" for="file">
                        浏览文件
                    </label>
                    <input type="file" class="form-control" id="file" name="file" value="" style="display:none" />
                </div>
            </div>
            
            <div class="form-group">
                <button type="button" class="btn btn-success js-save-btn">提交保存</button>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block script %}
<script type="text/javascript">
;(function($) {
    "use strict";
    
    $('#file').change(function() {
        $('#location').val($(this).val());
    });
    
    // 保存
    $(".js-save-btn").click(function(e) {
        e.stopPropagation;
        e.preventDefault;

        var files = $('#file')[0].files
        if (files.length <= 0) {
            return layer.msg("请选择要上传的文件");
        }

        var fd = new FormData()
        fd.append('file', files[0])
        fd.append('path', '{{ path }}')

        var url = '/file/upload';
        
        $.ajax({
            url: url,
            type: 'POST',
            data: fd,
            cache: false,
            processData: false,
            contentType: false,
            success: function(data) {
                if (data.code == 0) {
                    layer.msg("上传文件成功");
                    
                    setTimeout(function() {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }, 1500);
                } else {
                    layer.msg(data.message);
                }
            },
            error: function() {
                layer.msg("请求失败");
            }
        });
    });
})(jQuery);
</script>
{% endblock %}